package gropack;
import java.util.Stack;

public class ExprCalc implements Visitor {
	Stack<UnaryExpr> pile;
	
	public ExprCalc(){
		pile = new Stack<UnaryExpr>();
	}
	
	@Override
	public void visitIntExpr(IntExpr e) {
		pile.push(e);
	}
	@Override
	public void visitFloatExpr(FloatExpr e) {
		pile.push(e);
	}
	
	@Override
	public void visitPlusExpr(PlusExpr e) {
		(e.getLeft()).accept(this);
		(e.getRight()).accept(this);
		UnaryExpr result = pile.pop();
		
		result = result.plus(pile.pop());
		pile.push(result);
	}
	
	@Override
	public void visitMoinsExpr(MoinsExpr e) {
		(e.getLeft()).accept(this);
		(e.getRight()).accept(this);
		UnaryExpr result = pile.pop();

		result = result.moins(pile.pop());
		pile.push(result);
	}
	
	@Override
	public void visitDivExpr(DivExpr e) {
		(e.getLeft()).accept(this);
		(e.getRight()).accept(this);
		UnaryExpr result = pile.pop();

		result = result.div(pile.pop());
		pile.push(result);
	}
	
	@Override
	public void visitMultExpr(MultExpr e) { 
		(e.getLeft()).accept(this);
		(e.getRight()).accept(this);
		UnaryExpr result = pile.pop();

		result = result.mult(pile.pop());
		pile.push(result);		
	}
	
	@Override
	public void visitExpr(Expr e) {
		
	}
	
	public Expr result(){
		return pile.pop();
	}

}
